package com.osig.ec.cfca;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import cfca.paperless.client.bean.FieldBean;
import cfca.paperless.client.bean.ImageBean;
import cfca.paperless.client.bean.TextBean;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessUniteClient;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;

public class CfcaTestSyn {

    static String templateCode = "5";

    public static void main(String[] args) throws Exception {

    	/*PaperlessUniteClient clientBean = new PaperlessUniteClient("http://192.168.6.144:8080/PaperlessServer/PaperlessUniteServlet");
        clientBean.initSSLContext(PaperlessConfig.keyStorePath, PaperlessConfig.keyStorePassword, PaperlessConfig.trustStorePath, PaperlessConfig.trustStorePassword);
       
        String fieldBeanListXml = PaperlessUtil.buildFieldBeanListXml(getFieldBeanList());//true代表部分合成是部分flatten,默认为false
        //System.out.println(fieldBeanListXml);
        //String textBeanListXml = PaperlessUtil.buildTextBeanListXml(getTextBeanList());
        //String imageBeanListXml = PaperlessUtil.buildImageBeanListXml(getImageBeanList());
        
        String pdf = clientBean.addSynthesizeAutoTemplateParams(templateCode,fieldBeanListXml, "", "");
        IoUtil.write("D://synthesizeAutoTemplate.pdf", pdf.getBytes());
        //String errorRsString = PaperlessUtil.isError(pdf);
        if ("".equals(pdf)) {
            System.out.println("ok");
            IoUtil.write("D://synthesizeAutoTemplate.pdf", pdf.getBytes());
        } else {
            System.out.println(pdf);
        }*/
        
    	
    	 PaperlessClient clientBean = new PaperlessClient("http://192.168.6.144:8080/PaperlessServer/PaperlessUniteServlet");
         /*clientBean.initSSLContext(PaperlessConfig.keyStorePath, PaperlessConfig.keyStorePassword, PaperlessConfig.trustStorePath, PaperlessConfig.trustStorePassword);*/
        
         String fieldBeanListXml = PaperlessUtil.buildFieldBeanListXml(getFieldBeanList());//true代表部分合成是部分flatten,默认为false
         //System.out.println(fieldBeanListXml);
         //String textBeanListXml = PaperlessUtil.buildTextBeanListXml(getTextBeanList());
         //String imageBeanListXml = PaperlessUtil.buildImageBeanListXml(getImageBeanList());
         
         byte[] pdf = clientBean.synthesizeAutoTemplate(templateCode,fieldBeanListXml, "", "",  "00001", "00002");
         String errorRsString = PaperlessUtil.isError(pdf);
         if ("".equals(errorRsString)) {
             System.out.println("ok");
             IoUtil.write("D://synthesizeAutoTemplate.pdf", pdf);
         } else {
             System.out.println(new String(pdf, "UTF-8"));
         }
         
    }

    private static List<FieldBean> getFieldBeanList() throws Exception {
        List<FieldBean> fieldBeanList = new ArrayList<FieldBean>();
        fieldBeanList.add(new FieldBean("name_applicant", "张三", "", "text"));
        fieldBeanList.add(new FieldBean("cardNO_applicant", "213456789", "", "text"));
        fieldBeanList.add(new FieldBean("card_applicant1", "是", "", "checkbox"));
        fieldBeanList.add(new FieldBean("card_applicant2", "", "", "checkbox"));
        fieldBeanList.add(new FieldBean("card_applicant3", "", "", "checkbox"));
        fieldBeanList.add(new FieldBean("year_applicant", "2017", "", "text"));
        fieldBeanList.add(new FieldBean("mouth_applicant", "11", "", "text"));
        fieldBeanList.add(new FieldBean("day_applicant", "24", "", "text"));
        fieldBeanList.add(new FieldBean("name_org", "某某机构", "", "text"));
        fieldBeanList.add(new FieldBean("license_org", "1234567890 20501212", "", "text"));
        fieldBeanList.add(new FieldBean("legalpersion", "李四", "", "text"));
        fieldBeanList.add(new FieldBean("card_legal1", "是", "", "checkbox"));
        fieldBeanList.add(new FieldBean("card_legal2", "", "", "checkbox"));
        fieldBeanList.add(new FieldBean("card_legal3", "", "", "checkbox"));
        fieldBeanList.add(new FieldBean("license_org", "1234567890", "", "text"));
        fieldBeanList.add(new FieldBean("card_legal1", "是", "", "checkbox"));
        fieldBeanList.add(new FieldBean("card_legal2", "", "", "checkbox"));
        fieldBeanList.add(new FieldBean("card_legal3", "", "", "checkbox"));
        fieldBeanList.add(new FieldBean("cardNO_legal", "1234567890", "", "text"));
        fieldBeanList.add(new FieldBean("year_legal", "2017", "", "text"));
        fieldBeanList.add(new FieldBean("mouth_legal", "11", "", "text"));
        fieldBeanList.add(new FieldBean("day_legal", "24", "", "text"));
        fieldBeanList.add(new FieldBean("name_applicant1", "张三", "", "text"));
        fieldBeanList.add(new FieldBean("bank1", "建设", "", "text"));
        fieldBeanList.add(new FieldBean("bank2", "北京", "", "text"));
        fieldBeanList.add(new FieldBean("bank3", "地坛", "", "text"));
        fieldBeanList.add(new FieldBean("account", "3456789012", "", "text"));
        fieldBeanList.add(new FieldBean("RMB_capital", "伍拾陆万柒千玖佰捌拾壹", "", "text"));
        fieldBeanList.add(new FieldBean("RMB_lower", "567981", "", "text"));
        
        fieldBeanList.add(new FieldBean("year_apply", "2017", "", "text"));
        fieldBeanList.add(new FieldBean("mouth_apply", "11", "", "text"));
        fieldBeanList.add(new FieldBean("day_apply", "24", "", "text"));
        
        return fieldBeanList;
    }
    
/*    private static List<TextBean> getTextBeanList() throws Exception {
        List<TextBean> textBeanList = new ArrayList<TextBean>();
        TextBean textBean1 = new TextBean("你好啊\r\n我的啊\r\ndddddd", "", "银行签章：", "R", 0, 0);
        textBean1.setTextFontSize("15");
        textBean1.setTextColor("aa000000");
        TextBean textBean2 = new TextBean("545454545452212323\r\ndddddddddddddddddddddddd\r\n打开房间打开附件\r\ndkjfdkjf你的额尔", "", "单据号：", "R", 0, 0);
//        textBean2.setSpacingHeight(20);//设置行间距默认15像素
        textBean2.setTextFontSize("15");
        textBean2.setTextColor("aa000000");
        textBeanList.add(textBean1);
        textBeanList.add(textBean2);

        return textBeanList;
    }*/
    
/*    private static List<ImageBean> getImageBeanList() throws Exception {
        List<ImageBean> imageBeanList = new ArrayList<ImageBean>();
        // 如下代码是在pdf中添加可以显示的图片
        byte[] imageData = IoUtil.read(new FileInputStream("./TestData/seal.png"));
        ImageBean imageBean1 = new ImageBean("100", "100", "盖章", "C", 0, 0, imageData,3);//传入图片数据
        imageBeanList.add(imageBean1);
        
        String imagePath = Base64.toBase64String("/temp/2.png".getBytes("utf-8"));
        ImageBean imageBean2 = new ImageBean("100", "100", 1, "400", "300", imagePath,2);//传入路径
        imageBean2.setImagePath(imagePath);
        imageBeanList.add(imageBean2);
        
        ImageBean imageBean3 = new ImageBean("100", "100", 1, "400", "500");
        imageBean3.setSealImageCode("zjrz");//传入印模编码
        imageBeanList.add(imageBean3);
        return imageBeanList;
    }*/
}
